//
//  DeviceManageViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/5/4.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire

class DeviceManageViewController: BaseViewController {

    @IBOutlet var gpsView: UIView!
    @IBOutlet var addressLabel: UILabel!
    
    @IBOutlet var detailsAddressLabel: UILabel!
    
    var gpsmodel:GPSModel?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        requestGetGpsDeviceInfo()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        baseSetNavRightButtonWithTitle("添加")
    }
    
    override func baseNavRightButtonPressed(_ button: UIButton) {
        if self.gpsView.isHidden == true {
            let addGpsVC = AddGpsDeviceViewController()
            addGpsVC.title = "添加GPS设备"; self.navigationController?.pushViewController(addGpsVC, animated: true)
        }else {
            MBProgressHUD.alertShow(in: self.view, message: "设备已添加，请不要重复添加", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
        }
    }
    
    func updateGpsView() -> Void {
        addressLabel.text = self.gpsmodel?.name
        detailsAddressLabel.text = self.gpsmodel?.detail
    }

    @IBAction func editBtnPress(_ sender: UIButton) {
        let editGpsVC = AddGpsDeviceViewController()
        editGpsVC.title = "编辑GPS设备";
        editGpsVC.gpsmodel = self.gpsmodel
        self.navigationController?.pushViewController(editGpsVC, animated: true)
    }
    
    //获取Gps设备信息
    func requestGetGpsDeviceInfo() -> Void {

        Alamofire.request(getGpsDeviceInfoUrl, method:.post, parameters:["sid":UserModel.shareUser.data?.storeId ?? String()])
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    
                    var basemodel = BaseResponseOfModel<BaseArrayData<GPSModel>>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        if (basemodel.data?.data?.count)! > 0 {
                            self.gpsView.isHidden = false
                            let gpsmodel = basemodel.data?.data![0]
                            self.gpsmodel = gpsmodel
                            self.updateGpsView()
                        }
                        print("获取成功")
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                    
                case false:
                    print(response.result.error!)
                }
                
        }
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
